package com.ruoyi.biz.organization.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.biz.organization.domain.BizCompany;
import com.ruoyi.biz.organization.mapper.BizCompanyMapper;
import com.ruoyi.biz.organization.service.IBizCompanyService;
import com.ruoyi.common.core.domain.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 项目信息Service业务层处理
 *
 * @author Fei.Cong
 * @date 2023-04-21
 */
@Slf4j
@Service
public class BizCompanyServiceImpl extends ServiceImpl<BizCompanyMapper, BizCompany> implements IBizCompanyService {

    @Autowired
    private BizCompanyMapper bizCompanyMapper;

    /**
     * 分页查询项目信息
     *
     * @param page
     * @param bizCompany 项目信息
     */
    @Override
    public IPage<BizCompany> pageList(BizCompany bizCompany, IPage<BizCompany> page) {
        return bizCompanyMapper.selectPageList(page, bizCompany);
    }

    @Override
    public void remoteAdd(JSONObject jsonObject) {
        SysUser user = jsonObject.getObject("user", SysUser.class);

        BizCompany company = this.getOne(new LambdaQueryWrapper<BizCompany>()
                .eq(BizCompany::getOrganizationCode, user.getUserName())
                .eq(BizCompany::getDeleteFlag, 0)
                .last(" limit 1 "));

        String organizationCode = StringUtils.isBlank(jsonObject.getString("CORPCODE")) ? jsonObject.getString("IDCARD") : jsonObject.getString("CORPCODE");
        String organizationName = StringUtils.isBlank(jsonObject.getString("CORPNAME")) ? jsonObject.getString("PERSONNAME") : jsonObject.getString("CORPNAME");
        String linkMan = StringUtils.isBlank(jsonObject.getString("LINKMAN")) ? jsonObject.getString("PERSONNAME") : jsonObject.getString("LINKMAN");

        if (Objects.isNull(company)) {
            BizCompany entity = new BizCompany();
            entity.setCompanyId(IdUtil.randomUUID());
            entity.setUserId(user.getUserId());
            entity.setOrganizationCode(organizationCode);
            entity.setOrganizationName(organizationName);
            entity.setProvinceNum(jsonObject.getString("PROVINCENUM"));
            entity.setCityNum(jsonObject.getString("CITYNUM"));
            entity.setCountyNum(jsonObject.getString("COUNTYNUM"));
            entity.setAddress(jsonObject.getString("ADDRESS"));
            entity.setLegalMan(jsonObject.getString("LEGALMAN"));
            entity.setLegalManIdcard(jsonObject.getString("LEGALMANIDCARD"));
            entity.setLinkMan(linkMan);
            entity.setLinkPhone(jsonObject.getString("LINKPHONE"));
            entity.setDeleteFlag("0");
            boolean save = this.save(entity);
            log.info("锁号：{}，企业：{},{},保存状态：{}", jsonObject.getString("LOCKNUMBER_NO"), entity.getOrganizationCode(), entity.getOrganizationName(), save);
        } else {
            company.setUserId(user.getUserId());
            company.setOrganizationCode(organizationCode);
            company.setOrganizationName(organizationName);
            company.setProvinceNum(jsonObject.getString("PROVINCENUM"));
            company.setCityNum(jsonObject.getString("CITYNUM"));
            company.setCountyNum(jsonObject.getString("COUNTYNUM"));
            company.setAddress(jsonObject.getString("ADDRESS"));
            company.setLegalMan(jsonObject.getString("LEGALMAN"));
            company.setLegalManIdcard(jsonObject.getString("LEGALMANIDCARD"));
            company.setLinkMan(linkMan);
            company.setLinkPhone(jsonObject.getString("LINKPHONE"));
            boolean update = this.updateById(company);
            log.info("锁号：{}，企业：{},{},更新状态：{}", jsonObject.getString("LOCKNUMBER_NO"), company.getOrganizationCode(), company.getOrganizationName(), update);
        }
    }

}
